package com.liuda066.forumssm.services.impl;

import com.liuda066.forumssm.common.AppResult;
import com.liuda066.forumssm.common.ResultCode;
import com.liuda066.forumssm.dao.MessageMapper;
import com.liuda066.forumssm.exception.ApplicationException;
import com.liuda066.forumssm.model.Message;
import com.liuda066.forumssm.model.User;
import com.liuda066.forumssm.services.IMessageService;
import com.liuda066.forumssm.services.IUserService;
import com.liuda066.forumssm.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/9/5 22:49
 */
@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;

    @Resource
    private IUserService iUserService;

    @Override
    public void create(Message message) {
        if (message ==null || message.getPostUserId() == null
                || message.getReceiveUserId() == null || StringUtils.isEmpty(message.getContent())){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User user = iUserService.selectById(message.getReceiveUserId());
        if (user == null || user.getDeleteState() != 0){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);

        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);

        int row = messageMapper.insertSelective(message);
        if (row != 1){
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public Integer selectUnreadCount(Long userId) {
        if (userId == null){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Integer integer = messageMapper.selectUnreadCount(userId);
        return integer;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if (receiveUserId == null){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        return messageMapper.selectByReceiveUserId(receiveUserId);
    }

    @Override
    public Message selectById(Long id) {
        if  (id == null){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        return messageMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if  (id == null || state ==null){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Message message = new Message();
        message.setId(id);
        message.setState(state);

        messageMapper.updateByPrimaryKeySelective(message);
    }

    @Override
    public void reply(Long repliedId, Message message) {
        if (repliedId == null || repliedId <=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message existsMessage = messageMapper.selectByPrimaryKey(repliedId);
        if (existsMessage == null || existsMessage.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        log.warn("--------------reply------------------");
        updateStateById(repliedId, (byte) 2);
        create(message);
    }
}
